﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Survey
{
    public partial class SurveyCatalog : System.Web.UI.Page
    {
        private SurveyProcess surveyProcess;
        private string catalogName;
        private int result;
        private List<Entity.SurveyCatalog> listCatalog;
        private const bool blnAllowPaging = true;
        private const int iPageSize = 5;
        private static PagedDataSource pgdProducts;
        protected void Page_Load(object sender, EventArgs e)
        {
            surveyProcess = new SurveyProcess();
            pgdProducts = new PagedDataSource();
            if (!Page.IsPostBack)
            {
                this.getAllSurveyCatalog();
            }
        }

        // Get all survey catalog and display 
        private void getAllSurveyCatalog()
        {
            listCatalog = surveyProcess.getAllSurveyCatalog();

            pgdProducts.AllowPaging = blnAllowPaging;
            pgdProducts.PageSize = iPageSize;
            pgdProducts.DataSource = listCatalog;

            pgdProducts.CurrentPageIndex = ThisPageNumber;
            fillTotalPage(pgdProducts.PageCount);

            allCatalog.DataSource = pgdProducts;
            allCatalog.DataBind();
        }

        private int ThisPageNumber
        {
            get
            {
                object obj = ViewState["ThisPageNumber"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                this.ViewState["ThisPageNumber"] = value;
            }
        }

        // Calculations page number of list survey catalog and display
        private void fillTotalPage(int iTotalPages)
        {
            List<int> pageID = new List<int>();
            for (int i = 1; i <= iTotalPages; i++)
            {
                pageID.Add(i);
            }
            totalPage.DataSource = pageID;
            totalPage.DataBind();
        }

        // Create survey catalog when button click
        protected void submitCreateCatalog_Click(object sender, EventArgs e)
        {
            catalogName = SurveyCatalogName.Text;
            if (catalogName == "" || catalogName == null || catalogName.Length < 4)
            {
                if (catalogName.Length < 4)
                {
                    status.ForeColor = System.Drawing.Color.Red;
                    status.Text = "Can't be less than 4 characters!";
                }
                else
                {
                    status.ForeColor = System.Drawing.Color.Red;
                    status.Text = "Please enter survey catalog name!";
                }
            }
            else
            {
                result = surveyProcess.createNewCatalogSurvey(catalogName);
                if (result == 10)
                {
                    status.ForeColor = System.Drawing.Color.Black;
                    status.Text = "Create survey catalog success!";
                    this.getAllSurveyCatalog();
                }
                else if (result == 20)
                {
                    status.ForeColor = System.Drawing.Color.Red;
                    status.Text = "Can't connect to server!";
                }
                else if (result == 30)
                {
                    status.ForeColor = System.Drawing.Color.Red;
                    status.Text = "Survey catalog name is ready exits!";
                }
                else
                {
                    status.ForeColor = System.Drawing.Color.Red;
                    status.Text = "Unspecified error!";
                }
                SurveyCatalogName.Text = null;
            }
        }

        // Edit survey catalog name when button click
        protected void btEditCatalogName_Click(object sender, EventArgs e)
        {
            catalogName = txtCatalogName.Text;
            if (catalogName == "" || catalogName == null || catalogName.Length < 4)
            {
                if (catalogName.Length < 4)
                {
                    lbStatus.Text = "Can't be less than 4 characters!";
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    lbStatus.Text = "Please enter survey catalog name!";
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                }
            }
            else
            {
                result = surveyProcess.updateSurveyCatalog(Convert.ToInt32(hdIDSurveyCatalog.Value), catalogName);
                if (result == 10)
                {
                    lbStatus.ForeColor = System.Drawing.Color.Black;
                    lbStatus.Text = "Update survey catalog success!";
                    this.getAllSurveyCatalog();
                }
                else if (result == 20)
                {
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                    lbStatus.Text = "Can't connect to server!";
                }
                else if (result == 40)
                {
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                    lbStatus.Text = "Survey catalog name is ready exits!";
                }
                else
                {
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                    lbStatus.Text = "Unspecified error!";
                }
            }
        }

        // Select survey catalog for edit function and remove survey catalog
        protected void allCatalog_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int idSurveyCatalog = Convert.ToInt32(e.CommandArgument);
            //Select survey catalog for edit function
            if (e.CommandName == "Edit")
            {
                if (idSurveyCatalog == 1)
                {
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                    lbStatus.Text = "Can't delete Vote Student Survey!";
                }
                else
                {
                    string catalogName = surveyProcess.getSurveyCatalogName(idSurveyCatalog);
                    if (catalogName != "" || catalogName != null)
                    {
                        hdIDSurveyCatalog.Value = e.CommandArgument.ToString();
                        txtCatalogName.Enabled = true;
                        txtCatalogName.Text = catalogName;
                    }
                }
            }
            //Remove survey catalog
            else if (e.CommandName == "Delete")
            {
                if (idSurveyCatalog == 1)
                {
                    lbStatus.ForeColor = System.Drawing.Color.Red;
                    lbStatus.Text = "Can't delete Vote Student Survey!";
                }
                else
                {
                    int result = surveyProcess.deleteSurveyCatalogById(idSurveyCatalog);
                    if (result == 10)
                    {
                        lbStatus.ForeColor = System.Drawing.Color.Black;
                        lbStatus.Text = "Delete survey catalog success!";
                        this.getAllSurveyCatalog();
                    }
                    else
                    {
                        lbStatus.ForeColor = System.Drawing.Color.Red;
                        lbStatus.Text = "An error in delete process!";
                    }
                }
            }
        }

        //Change page when user click to page number button
        protected void PageNumber_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            ThisPageNumber = Convert.ToInt32(lbuton.Text) - 1;
            getAllSurveyCatalog();
        }

        //Change page when user click to prev or next number button
        protected void btPrev_Click(object sender, EventArgs e)
        {
            LinkButton lbuton = (LinkButton)sender;
            //Change page when user click to prev number button
            if (lbuton.ID == "btPrev")
            {
                if (ThisPageNumber > 0)
                {
                    ThisPageNumber = ThisPageNumber - 1;
                    getAllSurveyCatalog();
                }
            }
            //Change page when user click to next number button
            else if (lbuton.ID == "btNext")
            {
                if (ThisPageNumber < totalPage.Items.Count - 1)
                {
                    ThisPageNumber = ThisPageNumber + 1;
                    getAllSurveyCatalog();
                }
            }
        }
    }
}